package com.bobo.nutzbook;

import java.util.Date;

import org.apache.commons.mail.HtmlEmail;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.ioc.Ioc;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

import com.bobo.nutzbook.bean.User;

public class MainSetup implements Setup {

	public void init(NutConfig nc) {
		
		Ioc ioc = nc.getIoc();
		Dao dao = ioc.get(Dao.class);
		Daos.createTablesInPackage(dao, "com.bobo.nutzbook", false);
		
		// 初始化默认根用户
        if (dao.count(User.class) == 0) {
            User user = new User();
            user.setName("admin");
            user.setPassword("123456");
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            dao.insert(user);
        }
		
	}

	public void destroy(NutConfig nc) {
		// TODO Auto-generated method stub
	}
	
	/**
	 * 务必删除这段测试代码，否则每次启动都发送一封邮件，容易被封账号。
	 * @param ioc
	 */
	private void init_mail(Ioc ioc)
	{
		// 测试发送邮件
        try {
            HtmlEmail email = ioc.get(HtmlEmail.class);
            email.setSubject("测试NutzBook");
            email.setMsg("This is a test mail ... :-)" + System.currentTimeMillis());
            email.addTo("bbs2onlygb@126.com");//请务必改成您自己的邮箱啊!!!
            email.buildMimeMessage();
            email.sendMimeMessage();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}

}
